//
//  UserHeaderView.m
//  Guazi
//
//  Created by koolearn on 11-8-4.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UserHeaderView.h"
#import "ImageStore.h"
#import "GuaziAppDelegate.h"
#import "FriendDetailViewController.h"
#import "QuartzUtils.h"


@interface UserHeaderView (private)
- (UIImage*)getProfileImage:(NSString*)url isLarge:(BOOL)flag;
@end

@implementation UserHeaderView

@synthesize user;
@synthesize linkToProfileView;


#pragma mark - init & destroy methods

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.user = nil;
        photoButton = nil;
        self.linkToProfileView = YES;
    }
    return self;
}


- (id) initWithUser:(User *)auser
{
    self = [super initWithFrame:CGRectZero];
    if ( self ) {
        self.linkToProfileView = YES;
        [self updateUser:auser];
    }
    return self;
}


- (void) updateUser:(User *)auser
{
    self.user = auser;
    
    UIColor* backgroundColor = [UIColor colorWithRed:0.816f green:0.871f blue:0.957f alpha:1.0f];
    self.backgroundColor = backgroundColor;
    
    photoButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    UIImage* image = [self getProfileImage:user.profileImageUrl_s isLarge:NO];
    [photoButton setImage:image forState:UIControlStateNormal];
    if ( linkToProfileView ) {
        [photoButton addTarget:self action:@selector(showProfile) forControlEvents:UIControlEventTouchUpInside];
    }
    [self addSubview:photoButton];
    
    UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 250, 20)];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.text = user.screenName;
    [self addSubview:nameLabel];
    [nameLabel release];
    
    UILabel* phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 250, 20)];
    phoneLabel.backgroundColor = [UIColor clearColor];
    phoneLabel.font = [UIFont systemFontOfSize:12];
    phoneLabel.textColor = [UIColor colorWithRed:0.41f green:0.41f blue:0.41f alpha:1.0f];
    phoneLabel.text = user.phone;
    [self addSubview:phoneLabel];
    [phoneLabel release];
}


- (void)dealloc
{
    [user release];
    [photoButton release];
    [super dealloc];
}


 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor* color = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f];
    drawLine( context, 1, color.CGColor, CGPointMake(0, 69), CGPointMake(320,69));
    color = [UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];
    drawLine( context, 1, color.CGColor, CGPointMake(0, 70), CGPointMake(320, 70));
}


#pragma mark - profile image methods

- (UIImage*)getProfileImage:(NSString*)url isLarge:(BOOL)flag
{
    ImageStore *store = [GuaziAppDelegate getAppDelegate].imageStore;
    UIImage *image = [store getProfileImage:url isLarge:flag delegate:self];
    return image;
}


- (void)profileImageDidGetNewImage:(UIImage*)image
{
    [photoButton setImage:image forState:UIControlStateNormal];
    [photoButton setNeedsDisplay];
}


#pragma mark - operation methods

- (void) showProfile
{
    FriendDetailViewController* controller = [[FriendDetailViewController alloc] initWithUser:user];
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] pushViewController:controller animated:YES];
    [controller release];
}


@end
